/**
 * Created by Zhouxiumin on 2017/3/28.
 */
import {BaseParser} from './BaseParser'
import * as cheerio from "cheerio";


export class SosoParser extends BaseParser {
  baseUrl: string = 'http://ssoonn.com';
  name: string = '九九漫画';
  intro: string = '九九漫画 热门漫画免费在线观看，最新漫画';
  readonly expr: string = 'ssoonn\.com';
  tags: Array<{ title: string, url: string }> = [
    {title: '搞笑', url: 'http://ssoonn.com/lists/2/@page/'},
    {title: '格斗', url: 'http://ssoonn.com/lists/3/@page/'},
    {title: "科幻", url: "http://ssoonn.com/lists/4/@page/"},
    {title: "剧情", url: "http://ssoonn.com/lists/5/@page/"},
    {title: "侦探", url: "http://ssoonn.com/lists/6/@page/"},
    {title: "竞技", url: "http://ssoonn.com/lists/7/@page/"},
    {title: "魔法", url: "http://ssoonn.com/lists/8/@page/"},
    {title: "神鬼", url: "http://ssoonn.com/lists/9/@page/"},
    {title: "校园", url: "http://ssoonn.com/lists/10/@page/"},
    {title: "惊栗", url: "http://ssoonn.com/lists/11/@page/"},
    {title: "厨艺", url: "http://ssoonn.com/lists/12/@page/"},
    {title: "伪娘", url: "http://ssoonn.com/lists/13/@page/"},
    {title: "卖肉", url: "http://ssoonn.com/lists/14/@page/"},
    {title: "冒险", url: "http://ssoonn.com/lists/15/@page/"},
    {title: "国漫", url: "http://ssoonn.com/lists/19/@page/"},
    {title: "港漫", url: "http://ssoonn.com/lists/20/@page/"},
    {title: "耽美", url: "http://ssoonn.com/lists/21/@page/"},
    {title: "经典", url: "http://ssoonn.com/lists/22/@page/"},
    {title: "欧美", url: "http://ssoonn.com/lists/23/@page/"},
    {title: "日文", url: "http://ssoonn.com/lists/24/@page/"},
    {title: "亲情", url: "http://ssoonn.com/lists/25/@page/"},
  ];
  readonly searchUrl = 'http://ssoonn.com/comicsearch/s.aspx?s=@key';


  constructor() {
    super()
  }

  static getDomain(s) {
    let sDS = "http://ssoonn.9eden.com/dm01/|http://ssoonn.9eden.com/dm02/|http://ssoonn.9eden.com/dm03/|http://ssoonn.9eden.com/dm04/|http://ssoonn.9eden.com/dm05/|http://ssoonn.9eden.com/dm06/|http://ssoonn.9eden.com/dm07/|http://ssoonn.9eden.com/dm08/|http://ssoonn.9eden.com/dm09/|http://ssoonn.9eden.com/dm10/|http://ssoonn.9eden.com/dm11/|http://ssoonn.9eden.com/dm12/|http://ssoonn.9eden.com/dm13/|http://ssoonn.9eden.com/dm14/|http://ssoonn.9eden.com/dm15/|http://ssoonn.9eden.com/dm16/";
    let arrDS = sDS.split('|');
    let u = "";
    for (let i = 0; i < arrDS.length; i++) {
      if (s == (i + 1)) {
        u = arrDS[i];
        break;
      }
    }
    return u;
  }

  static unsuan(s) {
    let x = s.substring(s.length - 1);
    let xi = "abcdefghijklmnopqrstuvwxyz".indexOf(x) + 1;
    let sk = s.substring(s.length - xi - 12, s.length - xi - 1);
    s = s.substring(0, s.length - xi - 12);
    let k = sk.substring(0, sk.length - 1);
    let f = sk.substring(sk.length - 1);
    for (let i = 0; i < k.length; i++) {
      eval("s=s.replace(/" + k.substring(i, i + 1) + "/g,'" + i + "')");
    }
    let ss = s.split(f);
    s = "";
    for (let i = 0; i < ss.length; i++) {
      s += String.fromCharCode(ss[i]);
    }
    return s;
  }


  hots_parse(url, html) {
    let $ = cheerio.load(html);
    let list = [];
    let that = this;

    $(".pic img").each(function () {
      let img = $(this);

      let bm = {
        name: img.attr('alt'),
        url: that.urla(img.parent().attr('href')),
        logo: img.attr('src'),
      };
      list.push(bm);
    });
    return JSON.stringify(list);
  }

  tags_parse(url, html) {
    let $ = cheerio.load(html);
    let list = [];
    let that = this;
    $(".se-list").find("img").each(function () {
      let img = $(this);

      let bm = {
        name: img.parent().find('h3').text(),
        url: that.urla(img.parent().attr('href')),
        logo: img.attr('src'),
      };
      list.push(bm);
    });
    return JSON.stringify(list);
  }


  book_parse(url, html) {
    let $ = cheerio.load(html);
    let that = this;
    let data = {
      name: $('.con > h3').text(),
      author: html.match(/作者：(.+?)<\/p>/)[1],
      logo: $('.pic > img').attr('src'),
      updateTime: html.match(/更新日期：(.+?)<\/p>/)[1],
      intro: $('.ilist > p').text(),
      isSectionsAsc: 0,
      sections: [],
    };
    $("a.list_href").each(function () {
      let item = $(this);
      let bm = {name: item.text(), url: that.urla(item.attr('href'))};
      data.sections.unshift(bm);
    });
    return JSON.stringify(data);
  }

  section_parse(url, html) {
    let list = [];
    let sFiles = html.match(/sFiles\s*=\s*\"([^\"]*)\"/)[1];
    let sDomain = html.match(/sPath\s*=\s*\"([^\"]*)\"/)[1];
    sFiles = SosoParser.unsuan(sFiles);
    let arrFiles = sFiles.split("|");
    sDomain = SosoParser.getDomain(sDomain);
    for (let i = 0; i < arrFiles.length; i++) {
      list.push(sDomain + arrFiles[i]);
    }
    return JSON.stringify(list);
  }

  public search_parse(url, html) {
    return this.tags_parse(url, html);
  }

}
